package com.myzj.sportplay.controller;

import com.alibaba.fastjson.JSON;
import com.myzj.sportplay.bean.QueryInfo;
import com.myzj.sportplay.bean.User;
import com.myzj.sportplay.dao.UserDao;
import com.myzj.sportplay.util.IdGenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    SendMail sendMail;

    @Autowired
    private UserDao userDao;

    @RequestMapping("/allUser")
    public String getAllUser(QueryInfo queryInfo){
        // 获取查询总条数，当前编号
        int numbers = userDao.getUserCounts( queryInfo.getQuery() );//数据库的总数
        int pageSize = queryInfo.getPageSize();
        int pageStart = (queryInfo.getPageNum()-1)*pageSize;
        //判断总数是否足够分页，不够时修改pageStart为最后一页
        if(pageStart>=numbers){
            pageStart = numbers<=pageSize?0:(numbers/pageSize -1)*pageSize;
        }
        //获取当前页的用户数据
        List<User> users = userDao.getAllUser( queryInfo.getQuery() , pageStart, pageSize);
        HashMap<String,Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",users);
        return JSON.toJSONString(res);
    }

    @RequestMapping("/userState")
    public String updateUserState(@RequestParam("id")String id, @RequestParam("state")Boolean state){
        int i = userDao.updateState(id, state);
        return i>0 ? "success" : "error";
    }

    @RequestMapping("/addUser")
    public String addUser(@RequestBody User user){
        String res = "error";
        user.setId(IdGenUtil.uuid());
        user.setRole("普通用户");//默认普通用户
        user.setActiveCode(IdGenUtil.uuid());
        user.setState(false);//默认未启用
        int i = userDao.addUser(user);
        if(i>0){
            String activeCode = user.getActiveCode();
            // 准备激活链接
            String textHtml = "activation-account.html";
            String ipStr = "";
            try {
                ipStr = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                throw new RuntimeException(e);
            }
            String activationUrl ="http://"+ipStr+":9000/activeUser?activeCode="+activeCode;
            String sendFlag = sendMail.sendMsg(user.getEmail(),"注册账号激活",textHtml,activationUrl);
            if("1".equals(sendFlag)){
                res = "success";
            }
        }
        return res;
    }

    @RequestMapping("/activeUser")
    public String activeUser(String activeCode){
        int i = userDao.activeUser(activeCode);
        return i>0 ? "success" : "error";
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(String id){
        int i = userDao.deleteUser(id);
        return i>0 ? "success" : "error";
    }

    @RequestMapping("/getUpdateUser")
    public String getUpdateUser(String id){
        User user = userDao.getUpdateUser(id);
        return JSON.toJSONString(user);
    }

    @RequestMapping("/editUser")
    public String editUser(@RequestBody User user){
        int i = userDao.editUser(user);
        return i>0 ? "success" : "error";
    }

}
